<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <div class="login-head">
          <div class="logo"></div>
        </div>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item class="el-form" prop="account">
          <span class="svg-container">
            <i class="iconfont icon-crm_crm"></i>
          </span>
          <el-input v-model="form.account" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="password" >
          <span class="svg-container">
            <i class="iconfont icon-guanlian"></i>
          </span>
          <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="login" class="login" type="primary">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { reqLogin } from '@/api/user'
export default {
  name: 'Login',
  data () {
    return {
      form: {
        account: '',
        password: ''
      },
      rules: {
        account: [
          { required: true, message: '账号不能为空', trigger: ['blur', 'change'] },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    async login () {
      // 对整个表单进行校验
      try {
        await this.$refs.form.validate()
        console.log('登录校验成功')
      } catch {
        return console.log('登录校验失败')
      }
      // 校验成功
      // 发送ajax登录请求
      try {
        const res = await reqLogin(this.form.account, this.form.password)
        console.log(res)
        this.$message.success('登录成功')
        this.$router.push('/home')
      } catch {
        this.$message.error('手机号或者验证码错误')
      }
    }
  }
}
</script>

<style lang="less">
// 铺满整屏
.login-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("../../assets/bj-star.png") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 400px;
    padding: 30px 50px 10px;
    background-color: rgba(255, 255, 255, 0.2);
    .svg-container{
      position: absolute;
      top: 0px;
      left: 10px;
      z-index: 10;
      i{
        color: #DCDFE6;
      }
    }
    .el-input__inner{
      text-indent: 25px;
      color:  #DCDFE6;
      background-color: rgba(255, 255, 255, 0.2);
    }
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login {
      width: 100%;
    }
  }
}
</style>
